package com.agrimall.servlet.admin;

import com.agrimall.entity.User;
import com.agrimall.service.UserService;
import com.agrimall.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 管理员个人中心Servlet
 */
@WebServlet("/admin/user/profile")
public class AdminProfileServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("admin");
        
        // 验证管理员登录状态
        if (admin == null) {
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            return;
        }
        
        // 从数据库重新获取最新管理员信息（包括余额等）
        User latestAdmin = userService.getUserById(admin.getUserId());
        if (latestAdmin != null) {
            // 更新session中的管理员信息
            session.setAttribute("admin", latestAdmin);
            request.setAttribute("admin", latestAdmin);
        } else {
            request.setAttribute("admin", admin);
        }
        
        request.getRequestDispatcher("/admin/profile.jsp").forward(request, response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        User sessionAdmin = (User) session.getAttribute("admin");
        
        // 验证管理员登录状态
        if (sessionAdmin == null) {
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            return;
        }
        
        // 获取表单参数
        String realName = request.getParameter("realName");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        
        // 创建更新对象
        User updateAdmin = new User();
        updateAdmin.setUserId(sessionAdmin.getUserId());
        updateAdmin.setRealName(realName);
        updateAdmin.setPhone(phone);
        updateAdmin.setEmail(email);
        
        // 更新管理员信息
        boolean success = userService.updateUser(updateAdmin);
        
        if (success) {
            // 重新获取更新后的管理员信息
            User updatedAdmin = userService.getUserById(sessionAdmin.getUserId());
            if (updatedAdmin != null) {
                // 更新session中的管理员信息
                session.setAttribute("admin", updatedAdmin);
                request.setAttribute("admin", updatedAdmin);
                request.setAttribute("message", "个人信息修改成功！");
            } else {
                request.setAttribute("admin", sessionAdmin);
                request.setAttribute("error", "更新成功，但获取最新信息失败");
            }
        } else {
            request.setAttribute("admin", sessionAdmin);
            request.setAttribute("error", "个人信息修改失败，请重试");
        }
        
        request.getRequestDispatcher("/admin/profile.jsp").forward(request, response);
    }
}



